package com.cb.controller.member.user;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import com.cb.common.util.ConfigUtil;
import com.cb.core.controller.BaseListController;
import com.cb.domain.model.system.SystemUser;
import com.cb.domain.model.user.UserAccount;
import com.cb.domain.service.system.SystemUserManager;
import com.cb.domain.service.user.UserManager;

public class UserCommonController extends BaseListController{
	
	@Autowired
	private UserManager userManager;
	
	@Autowired
	private SystemUserManager systemUserManager;
	
	public void validateUserName(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String username=request.getParameter("userName");
		UserAccount account =userManager.findByUserName(username);
		String flag="false";
		if(account==null){
			SystemUser systemUser= systemUserManager.findByUserName(username);
			if(systemUser==null){
				flag="true";
			}
		}
		response.getWriter().print(flag);
	}
	
	
	public ModelAndView forgetPassword(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		return new ModelAndView("member/user/forgetPassword");
	}
	
	public ModelAndView findPassword(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		String username=request.getParameter("username");
		String findType=request.getParameter("findType");
		String msg="用户名不存在!请从新输入";
		Boolean autoLogin=false;
		if(StringUtils.isNotBlank(username)&&StringUtils.isNotBlank(findType)){

		UserAccount account =userManager.findByUserName(username);
			if(account!=null){
				autoLogin=true;
				if(Long.valueOf(findType)==1){
					
					String email="409468895@qq.com";
					
					String subject="密码找回";
					Map<String, Object> model=new HashMap<String, Object>();
					model.put("password", "123456");
//					model.put("linkUrl", link+user.getUserId()+"&key="+validCode.getCode());
					mailEngine.sendTemplateMail("sendRegisterEmail.vm", model, subject, null, 
							new String[]{email}, null, null, null, null, true);
					
					msg="请输入注册邮箱接收到的密码!";
				}else if(Long.valueOf(findType)==1){
					msg="请输入手机接收的密码";
				}
			}			
		}
		return new ModelAndView("member/user/forgetPassword").addObject("msg", msg).addObject("autoLogin", autoLogin);
	}
	
	public ModelAndView defaultAction(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView("front/user/userRegister");
	}

}
